package com.hanbang.mrcm.merchant;

import java.util.ArrayList;
import java.util.Collection;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.bean.RankData;
import com.hanbang.mrcm.myview.MyDialog;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyLog;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.SystemTools;
import com.hanbang.mrcm.utils.UiUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class SharedRankActivity extends BaseActivity {

	private PullToRefreshListView listView;
	private ArrayList<RankData> listDatas = new ArrayList<RankData>();
	private MyAdapter adapter = null;
	private int activityId = -1;

	// 1:分享 2：答题

	private int type = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activityId = getIntent().getIntExtra("activity_id", activityId);
		type = getIntent().getIntExtra("type", type);
		setContentView(R.layout.ranking);
		initView();
		setOnClickListener();
		getServiceData();
	}

	private void getServiceData() {
		String url = "";
		if (type == 1) {
			url = HttpInterfaces.shareRank;
		} else if (type == 2) {
			url = HttpInterfaces.answerRank;
		}
		httpUtils.send(HttpMethod.GET, url + "&activity_id=" + activityId,
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								Collection<RankData> c = JsonHelper.parseCollection(
										json.getJSONArray("result"),
										ArrayList.class, RankData.class);
								listDatas.addAll(c);
								adapter.notifyDataSetChanged();
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {

	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.right_text:
				getGuiZe();
				break;
			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		if (type == 2) {
			setTitle(findViewById(R.id.top_bar_title), "答题排名");
			((TextView) findViewById(R.id.type)).setText("答题积分");
		} else if (type == 1) {

			setTitle(findViewById(R.id.top_bar_title), "分享排名");
			((TextView) findViewById(R.id.type)).setText("分享次数");
		}

		TextView tv = (TextView) findViewById(R.id.right_text);
		tv.setText("规则");
		tv.setEnabled(true);
		tv.setVisibility(View.VISIBLE);
		UiUtils.setButtonClickTint(tv, R.color.white, R.color.gray_text);
		tv.setOnClickListener(onClickListener);
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setMode(Mode.DISABLED);

	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RankData data = (RankData) getItem(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.shared_rank_listitem, null);
				holder = new ViewHolder();
				holder.rank = (TextView) convertView.findViewById(R.id.rank);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.count = (TextView) convertView.findViewById(R.id.count);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.rank.setText(data.getRownum() + "");
			holder.name.setText(data.getUser_tel());
			if (type == 1) {
				holder.count.setText(data.getUser_count() + "");
			} else if (type == 2) {
				holder.count.setText(data.getUser_score().intValue() + "");
			}
			return convertView;
		}

		private class ViewHolder {
			TextView rank;
			TextView name;
			TextView count;
		}
	}

	private void getGuiZe() {
		httpUtils.configResponseTextCharset("UTF-8");
		httpUtils.send(HttpMethod.GET, HttpInterfaces.activityRule + "&type="
				+ type, new MyRequestCallBack<String>(this,
				getString(R.string.loadding)) {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				super.onSuccess(responseInfo);
				try {
					JSONObject json = new JSONObject(responseInfo.result);
					int status = json.getInt("status");
					if (status == 1) {
						JSONObject a = json.getJSONArray("result")
								.getJSONObject(0);
						String rule = a.getString("rule");
						/*
						 * 显示对话框
						 */
						MyDialog dialog = new MyDialog(SharedRankActivity.this,
								R.style.base_dialog, MyDialog.MODE.HINT_WEBVIEW);
						dialog.setTitle("规则");
						dialog.setWebViewContent(rule);
						dialog.show();
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
